﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Sispe.UI
{
    public partial class frmMttoExperienciaPostulante : Form
    {
        [Dependency]
        public IExperienciaPostulanteBL experienciaBL { get; set; }


        public frmMttoExperienciaPostulante()
        {
            InitializeComponent();
            this.BuildUp();
        }

        bool isNew;

        public POSTULANTE CurrPostulante { get; set; }

        public EXPERIENCIA_POSTULANTE CurrExperiencia { get; set; }


        public void NuevaExperiencia(string nombreCompleto)
        {
            isNew = true;
            eXPERIENCIA_POSTULANTEBindingSource.AddNew();

            if (CurrPostulante != null)
            {
                nombreCompletoLabel1.Text = nombreCompleto.ToString();

                idPostulanteTextBox.Text = CurrPostulante.IdPostulante.ToString();
            }
        }

        

        public void EditarExperiencia(string nombreCompleto)
        {
            isNew = false;
            eXPERIENCIA_POSTULANTEBindingSource.DataSource = CurrExperiencia;

            nombreCompletoLabel1.Text = nombreCompleto.ToString();

            idPostulanteTextBox.Text = CurrExperiencia.IdPostulante.ToString();


        }

        private void Guardar()
        {
            var experienciaPost = eXPERIENCIA_POSTULANTEBindingSource.Current as EXPERIENCIA_POSTULANTE;

            var msg = "ERROR";

            experienciaPost.Cargo = cargoTextBox.Text.ToUpper();
            experienciaPost.Experiencia = experienciaTextBox.Text.ToUpper();

            if (isNew == true)
            {
                try
                {
                    experienciaPost.IdPostulante = CurrPostulante.IdPostulante;

                    experienciaBL.AddExperienciaPostulante(experienciaPost);
                    MessageBox.Show("Registro Correcto", "Registro",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            if (isNew == false)
            {
                try
                {
                    experienciaBL.UpdateExperienciaPostulante(experienciaPost);

                    MessageBox.Show("Modificación Correcta", "Registro",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }


        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            Guardar();
        }

        private void cargoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var coma = Convert.ToString(",");
            var punto = Convert.ToString(".");
            var parentesis = Convert.ToString("()");
            var puntoComa = Convert.ToString(";");
            var slash = Convert.ToString("/");

            string cadena = coma + punto + parentesis + puntoComa + slash + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
